package com.ssh.action;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.ssh.entity.User;
import com.ssh.service.UserService;

public class UserAction extends ActionSupport implements ModelDriven<User>{
	private static final long serialVersionUID = -635040790332368558L;
	
	//注入UserService
	private UserService userService;
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	//模型驱动
	private User user=new User();
	@Override
	public User getModel() {
		return user;
	}
	
	//登录
	public String login(){
		User u=userService.login(user);
		if(u !=null){
			ServletActionContext.getRequest().getSession().setAttribute("u", u);
//			ActionContext.getContext().getSession().put("u", u);
			return "loginsuccess";
		}else{
			ServletActionContext.getRequest().getSession().setAttribute("error", "用户名或密码错误");
			return "loginfail";
		}
	}
	
	
	/**
	 * 退出
	 */
	public String loginout(){
		ServletActionContext.getRequest().getSession().invalidate();
		return "loginfail";
	}
	
}
